package com.supor.md_video.api;

import com.android.baseconfig.base.BaseApiResult;
import com.android.baseconfig.base.BaseRetrofit;
import com.android.baseconfig.common.config.HttpConstant;
import com.supor.md_video.entity.VideoCommListBean;
import com.supor.md_video.entity.VideoCommListResultBean;
import com.supor.md_video.entity.VideoCommThumbsBean;
import com.supor.md_video.entity.VideoFavoritBean;
import com.supor.md_video.entity.VideoListBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.rxjava3.core.Observable;
import okhttp3.RequestBody;

public class VideoApiImpl extends BaseRetrofit<VideoApiService> implements VideoApiService {
    private static VideoApiImpl instance;

    public static VideoApiImpl getInstance() {
        if (instance == null) {
            instance = new VideoApiImpl();
        }
        return instance;
    }

    @Override
    public Observable<BaseApiResult<VideoListBean>> getVideoList(String recipeStatus,
                                                                 String recipeType,
                                                                 String providerId,
                                                                 int limit,
                                                                 int page) {
        return api.getVideoList(recipeStatus,
                recipeType,
                providerId,
                limit,
                page).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<VideoCommListResultBean>> getVideoCommList(String addrId,
                                                                               int limit,
                                                                               int page,
                                                                               String replyAddr) {
        return api.getVideoCommList(
                addrId,
                limit,
                page, replyAddr).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<String>> commFilter(String text) {
        return api.commFilter(
                text).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<VideoCommListBean>> addComm(RequestBody requestBody) {
        return api.addComm(requestBody).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<VideoFavoritBean>> subRecipe(RequestBody requestBody) {
        return api.subRecipe(requestBody).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<VideoFavoritBean>> subRecipeDel(String id) {
        return api.subRecipeDel(id).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<VideoCommThumbsBean>> commThumbs(RequestBody requestBody) {
        return api.commThumbs(requestBody).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<String>> commThumbsDel(String id) {
        return api.commThumbsDel(id).compose(this.applySchedulers());
    }

    public Observable<BaseApiResult<VideoCommThumbsBean>> commThumbs(String addrId) {
        /*"addrId": "食谱 ID",
"bossUserId": "用户 UUID",
"providerId": "运营商编号",
"replyAddr": "点赞位置：RECIPE_ID：点赞的是食谱"*/
        JSONObject jsonObject = new JSONObject();
        RequestBody requestBody = null;
        try {
            jsonObject.put("addrId", addrId);
            jsonObject.put("replyAddr", "REPLY_ID");//REPLY_ID 评论
            jsonObject.put("providerId", 0);
            requestBody = RequestBody.create(HttpConstant.JSON,
                    jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return commThumbs(requestBody);
    }

    public Observable<BaseApiResult<VideoFavoritBean>> subRecipe(
            String addrId,
            String bossUserId
    ) {
          /*{
  "addContent": "收藏内容",
  "addrId": "收藏内容的ID，如果是食谱，就是食谱ID；如果是作品，就是作品ID",
  "bossUserId": "全局统一的用户UUID",
  "favoriteAddr": "区分收藏的类型",
  "providerId": "运营商ID，管理员提供",
  "replyContent": "收藏内容的描述",
  "replyTime": 收藏时间戳LONG
}*/
        JSONObject jsonObject = new JSONObject();
        RequestBody requestBody = null;
        try {
            jsonObject.put("addrId", addrId);
            jsonObject.put("bossUserId", bossUserId);
            jsonObject.put("favoriteAddr", "RECIPE_ID");
            jsonObject.put("providerId", 0);
            jsonObject.put("replyTime", System.currentTimeMillis());
            requestBody = RequestBody.create(HttpConstant.JSON,
                    jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return subRecipe(requestBody);

    }

    public Observable<BaseApiResult<VideoCommListBean>> addComm(String text, String bossUserId,
                                                                String addrId,String recipeId,
                                                                String replyContent, String replyTime) {
        JSONObject jsonObject = new JSONObject();
        RequestBody requestBody = null;
        try {
            jsonObject.put("text", text);
            jsonObject.put("addrId", addrId);
            jsonObject.put("bossUserId", bossUserId);
            jsonObject.put("providerId", 0);
            jsonObject.put("replyAddr", "RECIPE_ID");
            jsonObject.put("replyContent", replyContent);
            jsonObject.put("replyTime", replyTime);
            jsonObject.put("note", recipeId);
            requestBody = RequestBody.create(HttpConstant.JSON,
                    jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return addComm(requestBody);
    }


//    @Override
//    public Observable<BaseApiResult<UserInfo>> login(String mobile, String CAPTCHA,
//                                                     String providerId) {
//        return api.login(mobile, CAPTCHA, providerId).compose(this.applySchedulers());
//    }


    /**
     * 分享统计
     *
     * @param recipeId 食谱id
     * @return
     */
    public Observable<BaseApiResult<String>> shareUpdate(String recipeId) {
        Map<String, String> map = new HashMap<>();
        map.put("recipeId", recipeId);
        return shareUpdate(recipeId, map);
    }

    @Override
    public Observable<BaseApiResult<String>> shareUpdate(String id, Map<String, String> map) {
        return api.shareUpdate(id, map).compose(this.applySchedulers());
    }
}
